import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { FriendsService } from './friends.service';
import { SendFriendRequestDto } from './dto/friend-request.dto';
import { AuthGuard } from '../../common/guards/auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import type { JwtPayload } from '../auth/interfaces/jwt-payload.interface';

@Controller('friends')
@UseGuards(AuthGuard)
export class FriendsController {
  constructor(private readonly friendsService: FriendsService) {}

  /**
   * 发送好友请求
   * @param dto 发送好友请求的DTO
   * @param user 当前用户
   * @returns 发送好友请求的结果
   */
  @Post('request')
  sendRequest(
    @Body() dto: SendFriendRequestDto,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.sendFriendRequest(dto, user.sub);
  }

  /**
   * 接受好友请求
   * @param id 好友请求ID
   * @param user 当前用户
   * @returns 接受好友请求的结果
   */
  @Post('request/:id/accept')
  acceptRequest(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.acceptFriendRequest(id, user.sub);
  }

  /**
   * 拒绝好友请求
   */
  @Post('request/:id/reject')
  rejectRequest(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.rejectFriendRequest(id, user.sub);
  }

  /**
   * 删除好友
   */
  @Delete(':friendId')
  removeFriend(
    @Param('friendId', ParseIntPipe) friendId: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.removeFriend(friendId, user.sub);
  }

  /**
   * 拉黑用户
   */
  @Post('block/:userId')
  blockUser(
    @Param('userId', ParseIntPipe) userId: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.blockUser(userId, user.sub);
  }

  /**
   * 取消拉黑
   */
  @Delete('block/:userId')
  unblockUser(
    @Param('userId', ParseIntPipe) userId: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.unblockUser(userId, user.sub);
  }

  /**
   * 获取好友列表
   * @param page 页码
   * @param limit 每页条数
   * @returns 好友列表
   */
  @Get('list')
  getFriendsList(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.friendsService.getFriendsList(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 50,
    );
  }

  /**
   * 获取待处理的好友请求
   */
  @Get('requests/pending')
  getPendingRequests(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.friendsService.getPendingRequests(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }

  /**
   * 获取我发送的好友请求
   */
  @Get('requests/sent')
  getSentRequests(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.friendsService.getSentRequests(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }

  /**
   * 检查是否是好友
   */
  @Get('check/:userId')
  isFriend(
    @Param('userId', ParseIntPipe) userId: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.friendsService.isFriend(user.sub, userId);
  }

  /**
   * 搜索用户（用于添加好友）
   */
  @Get('search')
  searchUsers(
    @Query('keyword') keyword: string,
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.friendsService.searchUsers(
      keyword,
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }
}
